package cn.car91.activity;

import java.util.List;

import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import cn.car91.R;
import cn.car91.adapter.RepairShopListAdapter;
import cn.car91.bean.RepairShop;
import cn.car91.http.HttpHandler;
import cn.car91.pager.CollectionPager;
import cn.car91.utils.Car91JsonUtil;
import cn.car91.view.HerilyAlertDialog;
import cn.car91.view.MyGridView;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnItemClick;
import com.lidroid.xutils.view.annotation.event.OnItemLongClick;

public class CollectionRepairshopActivity extends BaseActivity {
    private CollectionPager repairShopPager = new CollectionPager();

    @ViewInject(R.id.repairshop_list_scroll)
    ScrollView scrollView;
    @ViewInject(R.id.repairshop_list)
    MyGridView repairShopList;
    @ViewInject(R.id.repairshop_list_none)
    View none;

    RepairShopListAdapter repairShopListAdapter = null;

    private boolean addDate = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        setContentView(R.layout.activity_user_collection_repairshop);
        super.onCreate(savedInstanceState);

        repairShopListAdapter = new RepairShopListAdapter(this, R.layout.view_repairshop_stream_item);
        repairShopList.setAdapter(repairShopListAdapter);

        getData();

        scrollView.setOnTouchListener(onScrollTouchListener);

        ((TextView) findViewById(R.id.header_text)).setText("收藏的商家");
    }

    @OnItemLongClick(R.id.repairshop_list)
    public boolean onItemLongClick(AdapterView<?> arg0, final View view,
			final int which, long arg3) {
		HerilyAlertDialog.Builder builder = new HerilyAlertDialog.Builder(CollectionRepairshopActivity.this);
		String[] controls = new String[]{"查看商家","取消收藏","评论"};
		final String rid = ((RepairShop)repairShopListAdapter.getItem(which)).getId();
        final String rname = ((RepairShop)repairShopListAdapter.getItem(which)).getName();
		builder.setTitle(rname);
		builder.setItems(controls, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				switch (arg1) {
				case 0:
			        Intent intent = new Intent(CollectionRepairshopActivity.this, RepairShopDetailActivity.class);
			        intent.putExtra("rid", rid);
			        startActivity(intent);
					break;
				case 1:
					HerilyAlertDialog.Builder builder = new HerilyAlertDialog.Builder(CollectionRepairshopActivity.this);
					builder.setMessage("确认取消对该商家的收藏");
					builder.setPositiveButton("确定", new OnClickListener() {
						@Override
						public void onClick(DialogInterface arg0, int arg1) {
		                    try {
		                    	HttpHandler.httpHandler.repairShopCollect(rid, getCar91Application().getUser().getId(), "d", new RequestCallBack() {
									@Override
									public void onFailure(HttpException arg0, String arg1) {
										Toast.makeText(CollectionRepairshopActivity.this, "请求异常", Toast.LENGTH_SHORT).show();;
									}

									@Override
									public void onSuccess(ResponseInfo arg0) {
										repairShopListAdapter.removeData(which);
										repairShopListAdapter.notifyDataSetChanged();
									}
		                    	});
		                    } catch (Exception e) {
		                    }
						}
					}).setNegativeButton("取消", null).create().show();
					break;
				case 2:
					Intent intent2 = new Intent(CollectionRepairshopActivity.this, RepairShopCommentActivity.class);
					intent2.putExtra("rid", rid);
					intent2.putExtra("rname", rname);
					startActivity(intent2);
					break;

				default:
					Intent intent3 = new Intent(CollectionRepairshopActivity.this, RepairShopDetailActivity.class);
			        intent3.putExtra("rid", rid);
			        startActivity(intent3);
					break;
				}
			}
		}).create().show();
		return true;
	}
    
    @OnItemClick(R.id.repairshop_list)
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                            long arg3) {
        String rid = repairShopListAdapter.getRepairShopId(arg2);

        Intent intent = new Intent();
        intent.putExtra("rid", rid);
        intent.setClass(CollectionRepairshopActivity.this, RepairShopDetailActivity.class);
        startActivity(intent);
    }

    private OnTouchListener onScrollTouchListener = new OnTouchListener() {

        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            // TODO Auto-generated method stub
            switch (arg1.getAction()) {
                case MotionEvent.ACTION_UP:
                    doOnBorderListener();
                    break;
            }
            return false;
        }

        private void doOnBorderListener() {

            if (repairShopList != null && repairShopList.getMeasuredHeight() <= scrollView.getScrollY() + scrollView.getHeight()) {
                //底部
                addDate = true;
                getData();
            } else if (scrollView.getScrollY() == 0) {
                //顶部
            }
        }
    };

    public void getData() {
    	repairShopPager.getCollectRepairShop(getCar91Application().getUser().getId(), callbackList);
    }

    /* 列表页面 */
    private void refreshListViewData(List data) {
        if (addDate) {
            repairShopListAdapter.addData(data);
        } else {
            repairShopList.removeAllViewsInLayout();
            repairShopListAdapter.setData(data);
        }
        repairShopListAdapter.notifyDataSetChanged();
        addDate = true;
    }

    /**
     * 回调获取商家列表
     */
    private RequestCallBack callbackList = new RequestCallBack() {

        @Override
        public void onStart() {
            // TODO Auto-generated method stub
            super.onStart();
            showTextWhenNone(false);
        }

        @Override
        public void onFailure(HttpException arg0, String arg1) {
            // TODO Auto-generated method stub
            Toast.makeText(CollectionRepairshopActivity.this, "网络异常~", Toast.LENGTH_SHORT).show();

            showTextWhenNone(false);
        }

        @Override
        public void onSuccess(ResponseInfo arg0) {
            // TODO Auto-generated method stub
            List<RepairShop> repairShops = Car91JsonUtil.getDTOList(arg0.result.toString(), RepairShop.class);

            if (repairShops.size() > 0) {
                showTextWhenNone(false);
            } else if (!addDate) {
                showTextWhenNone(true);
            }
            refreshListViewData(repairShops);
        }

        private void showTextWhenNone(Boolean show) {
            if (show) {
                if (scrollView.getVisibility() == View.VISIBLE) {
                    scrollView.setVisibility(View.GONE);
                }
                if (none.getVisibility() == View.GONE) {
                    none.setVisibility(View.VISIBLE);
                }
            } else {
                if (scrollView.getVisibility() == View.GONE) {
                    scrollView.setVisibility(View.VISIBLE);
                }
                if (none.getVisibility() == View.VISIBLE) {
                    none.setVisibility(View.GONE);
                }
            }
        }
    };
}
